////////////////////////////////////////////////////////
//	File:		ShopState.cpp
//	Class:		SGP
//	Updated:	3/6/2012
//	Author:		Roc Myers
//	Purpose:	To contain all Item Shop related code
////////////////////////////////////////////////////////
#include "ShopState.h"
#include "Game.h"
#include "MainMenuState.h"
#include "tinyxml\tinyxml.h"
#include "SGD Wrappers\CSGD_XAudio2.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_Direct3D.h"
#include "SGD Wrappers\CSGD_DirectInput.h"
ShopState* ShopState::pInstance = nullptr;

ShopState::ShopState(void)
{
	BGMID		= -1;
	FontID		= -1;
	CursorID	= -1;
	MPotions	= 0;
	HPotions	= 0;
	Inhalers	= 0;
	Gold		= 0;
}


ShopState::~ShopState(void)
{
}

ShopState* ShopState::GetInstance(void)
{
	if(pInstance == nullptr)
		pInstance = new ShopState();

	return pInstance;
}

void ShopState::DeleteInstance()
{
	if(pInstance != nullptr)
	{
		delete pInstance;
		pInstance = nullptr;
	}
}

void ShopState::Enter(void)
{
	Selection = 0;

	// Load resources
	CursorID	= CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/SGD_MenuCursor.png"));
	FontID		= CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/LARPFont.png"),D3DCOLOR_XRGB(0,0,0));
	BGMID		= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("resource/ShopMusic.xwm"));
	Font.Initialize(FontID,"LARPFont.fnt");

	// Set up sound
	CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(Game::GetInstance()->SfxVol);
	CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(Game::GetInstance()->BgmVol);
	CSGD_XAudio2::GetInstance()->MusicSetSongPan(BGMID, Game::GetInstance()->Pan);
	CSGD_XAudio2::GetInstance()->MusicPlaySong(BGMID, true);

	LoadFile(Game::GetInstance()->GetActiveFile());
}

bool ShopState::Input(void)
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN))
	{
		switch(Selection)
		{
		case 0:
			{
				Item* i = new Item(0);
				ItemPurchased(i);
				break;
			}

		case 1:
			{
				Item* i = new Item(1);
				ItemPurchased(i);
				break;
			}

		case 2:
			{
				Item* i = new Item(2);
				ItemPurchased(i);
				break;
			}
		};
	}
	return true;
}

void ShopState::Update(float fElapsedTime)
{
}

void ShopState::Render(void)
{
}

void ShopState::Exit(void)
{
	if(BGMID != -1)
	{
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(BGMID);
		BGMID = -1;
	}

	if(CursorID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(CursorID);
		CursorID = -1;
	}

	if(FontID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(FontID);
		FontID = -1;
	}

	DeleteInstance();
}

void ShopState::SaveInventory(const char* szFileName)
{

}

void ShopState::LoadFile(int FileNumber)
{

}

void ShopState::ItemPurchased(Item* i)
{
	switch(i->GetID())
	{
	case HPOTION:
		{
			HPotions += 1;
			Inventory.push_back(i);
			break;
		}

	case MPOTION:
		{
			MPotions += 1;
			Inventory.push_back(i);
			break;
		}

	case INHALER:
		{
			Inhalers += 1;
			Inventory.push_back(i);
			break;
		}

	default:
		break;
	};
}